//
//  XTDairyDetailsViewController.m
//  BeautifulHome
//
//  Created by dlios on 15-5-14.
//  Copyright (c) 2015年 ACE. All rights reserved.
//

#import "XTDairyDetailsViewController.h"
#import "XTNetworkHandle.h"
#import "UIImageView+WebCache.h"
@interface XTDairyDetailsViewController () <UIWebViewDelegate>
@property(nonatomic, retain)NSMutableArray *xqArr;
@property(nonatomic, retain)NSString *message;
//@property(nonatomic, retain)UIScrollView *scro;
@property(nonatomic, retain)UIWebView *web;
@end

@implementation XTDairyDetailsViewController

- (void)dealloc
{
    [_xqArr release];
    [_message release];
    //[_scro release];
    //防止crash
    _web.delegate = nil;
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"日记详情";
    //self.view.backgroundColor = [UIColor whiteColor];
    
    
    //获取数据
    [self getData];
    
    
    
}


- (void)getData
{
#warning 封装AFN的调用
    // 参数1: 网络接口
    // 参数2: 返回数据的Block 参数result中是网络请求的返回数据
    //获取网址
    NSString *str = [NSString stringWithFormat:@"http://www.to8to.com/mobileapp/zxdiary.php?action=getposts&type=2&p=1&pg=15&tid=%@&uid=0",self.tidXq];
    
    [XTNetworkHandle getDataWithURL:str completion:^(id result) {
        // result为请求接口的返回数据
        //NSLog(@"~~~~~%@", result);
        self.xqArr = [NSMutableArray array];
        NSMutableArray *arr = [result objectForKey:@"content"];
        NSDictionary *dic = [arr objectAtIndex:0];
        NSLog(@"!!!%@",dic);
        self.message = [dic objectForKey:@"message"];
        
        
        
        self.message = [self.message stringByReplacingOccurrencesOfString:@"_image" withString:@"image"];
        NSString *str = [NSString stringWithFormat:@"<img width=%lf src=\"", self.view.bounds.size.width - 20];
        self.message = [self.message stringByReplacingOccurrencesOfString:@"<image>" withString:str];
        self.message = [self.message stringByReplacingOccurrencesOfString:@"</image>" withString:@"\"></img>"];
        
        
        [self creatWebView];
        
    }];
    
}
- (void)creatWebView
{
    CGFloat f = [[self class]heightWithText:self.subjectXq];
    
    self.web = [[UIWebView alloc] initWithFrame:CGRectMake(10, 0, self.view.bounds.size.width - 20, self.view.bounds.size.height)];
    
    _web.scrollView.contentInset = UIEdgeInsetsMake(120 + f, 0, 0, 0);
    self.web.scrollView.backgroundColor = [UIColor whiteColor];
    _web.delegate = self;
    [_web loadHTMLString:self.message baseURL:nil];
    _web.hidden = YES;
    //允许WebView滚动
    _web.scrollView.scrollEnabled = YES;
    [self.view addSubview:_web];
    [self xiagqing];
}

+ (CGFloat)heightWithText:(NSString *)text
{
    //设置一个字典保存文本属性
    NSDictionary *dic =[ NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];
    //NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    
    //预设一个尺寸  文本最大不会超过这个尺寸
    CGSize size = CGSizeMake(375, 2000);
    //根据文本信息获得一个CGRect
    //参数1: 尺寸范围
    //参数2: 文本属性
    //参数3: 按照什么方式获取Rect
    //参数4: nil
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //返回得到的高度
    return rect.size.height;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
   //CGFloat f = [[self class]heightWithText:self.subjectXq];
//    [webView loadHTMLString:self.message baseURL:nil];
    //_scro.contentSize = CGSizeMake(self.view.frame.size.width,  webView.scrollView.contentSize.height  + 500 + f + 120);
    webView.hidden = NO;
    
}

- (void)xiagqing
{
    
    
    CGFloat f = [[self class]heightWithText:self.subjectXq];
    
    //头像
    UIImageView *imageViewHead = [[UIImageView alloc]initWithFrame:CGRectMake(30, 20 - 120 - f, 60, 60)];
    
    [imageViewHead sd_setImageWithURL:[NSURL URLWithString:self.avatarXq] placeholderImage:[UIImage imageNamed:@"p2209761319"]];
    imageViewHead.layer.masksToBounds = YES;
    [imageViewHead.layer setCornerRadius:30];
    [self.web.scrollView addSubview:imageViewHead];
    [imageViewHead release];
    
    //姓名
    UILabel *name1 = [[UILabel alloc]initWithFrame:CGRectMake(120, 25 - 120 - f, 200, 20)];
    //name1.backgroundColor = [UIColor greenColor];
    NSString *str = [NSString stringWithFormat:@"作者:  %@",self.authorXq];
    name1.text = str;
    [name1 setFont:[UIFont systemFontOfSize:17]];
    [self.web.scrollView addSubview:name1];
    
    //几分钟前
    UILabel *time1 = [[UILabel alloc]initWithFrame:CGRectMake(120, 55 - 120 - f, 200, 20)];
    //time1.backgroundColor = [UIColor greenColor];
    NSString *str1 = [NSString stringWithFormat:@"时间:  %@",self.datelineXq];
    time1.text = str1;
    [time1 setFont:[UIFont systemFontOfSize:17]];
    [self.web.scrollView addSubview:time1];
    
    //标题
    UILabel *title1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 100 - 120 - f, self.view.frame.size.width - 30, f + 20)];
    //[title1 setFont:[UIFont systemFontOfSize:20]];
    //title1.backgroundColor = [UIColor greenColor];
    NSString *str2 = [NSString stringWithFormat:@"    %@",self.subjectXq];
    title1.text = str2;
    title1.numberOfLines = 0;
    [title1 setLineBreakMode:NSLineBreakByCharWrapping];
    [self.web.scrollView addSubview:title1];
    
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
